//
//  BrowseView.m
//  MobileZIPCodes
//
//  Created by Andreas Schwelling on 23.11.07.
//	http://www.andi.de/zipcodes/
// 
//  Released under the GNU General Public License.
//  See LICENSE for details.
//
//  Use at your own risk.
//

#import "BrowseView.h"




@implementation ZipDetailView

- (id)initWithFrame:(struct CGRect)rect {
	
	self = [super initWithFrame:rect];
	
	_rect = rect;
	
	[_tableCells release];
	_tableCells = [[NSMutableArray alloc] init];
	
	[_tableHeaders release];	
	_tableHeaders = [[NSMutableArray alloc] init];
	
	NSMutableDictionary* cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
	
	NSString *countstr = [[_delegate getBdo] getCountry];
	NSString *tableheaderstr = [[[countstr stringByAppendingString:@" - ZIP code: "] stringByAppendingString:_zipprefix] stringByAppendingString:@"..."];
	
	[cellDict setObject:tableheaderstr forKey:@"title"];
	[cellDict setObject:[[NSNumber alloc] initWithInt:0] forKey:@"beginRow"];
	
	[_tableHeaders addObject: cellDict];
	[cellDict release];
	
	DataManager *dm = [DataManager alloc];
	
	SQLiteDB *sql = [[SQLiteDB alloc] initWithPath:[dm getDataBaseName:countstr]];
	
	[dm release];
	
	NSArray *nsa = [sql query:[NSString stringWithFormat:@"SELECT zipcode from zipcodes where zipcode like '%@%%' ORDER BY zipcode asc;",_zipprefix]];
	
	//NSLog(@"array: %@",nsa);
	[sql release];
	int i=0;
	for (i=0;i<[nsa count];i++) {
		
		
		
		//NSLog(@"object: %@",[[nsa objectAtIndex:i] objectForKey:@"state"]);
		UIImageAndTextTableCell* cell = [[UIImageAndTextTableCell alloc] init];
		[cell setTitle: [[nsa objectAtIndex:i] objectForKey:@"zipcode"]];
		[cell setDisclosureStyle:2];
		[cell setShowDisclosure:YES animated:NO];
		[_tableCells addObject: cell]; 
		[cell release];
		
		
		
	}
	
	
	
	_sectionList = [[UISectionList alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height) showSectionIndex:NO];
	[_sectionList setDataSource:self];
	
	[self addSubview:_sectionList];
	
	_table = [_sectionList table];
	[_table setShouldHideHeaderInShortLists:NO];
	UITableColumn * packageColumn = [[UITableColumn alloc] initWithTitle:@"Title" identifier:@"title" width:320.0f];
	
	[_table addTableColumn:packageColumn];
	[_table setSeparatorStyle:1];
	[_table setRowHeight:48.0f];
	[_table setDelegate:self];
	[_table setAllowsReordering:NO];
	[_table reloadData];
	[_sectionList reloadData];
	
	[packageColumn release];
	
	//	[[_delegate getNavBar] showLeftButton:@"Back" withStyle:2 rightButton:nil withStyle:0];	
	return self;
	
	
}

-(void)dealloc {
	[_tableHeaders release];
	[_tableCells release];
	//[_table release];
	[_sectionList release];	
	
	[super dealloc];
}

-(void)setDelegate:(id)del {
	//NSLog(@"delegate: %@",del);
	_delegate = del;
}

-(void)setZipPrefix:(NSString*)prefix {
	_zipprefix = prefix;
}

-(void)setPrevView:(UIView*)pv {
	_prevView = pv;
	//NSLog(@"NameStateView setPrevView: %@",_prevView);
	
}

-(UIView*)getPrevView {
	//NSLog(@"NameStateView getPrevView: %@",_prevView);
	return _prevView;
}

#pragma mark - delegated methods -

- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList {
	return [_tableHeaders count];
}

- (NSString *)sectionList:(UISectionList *)aSectionList titleForSection:(int)row {	
	return [[_tableHeaders objectAtIndex:row] objectForKey:@"title"];
}       

- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)row {
	// this is what determines the placement of the header
	return [[[_tableHeaders objectAtIndex:row] valueForKey:@"beginRow"] intValue];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
{
	return [_tableCells objectAtIndex: row];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
			   reusing: (BOOL) reusing
{
	//What does this message do?
    return nil;
}

- (int) numberOfRowsInTable: (UISectionTable*)table
{
	return [_tableCells count];
}

- (void) tableRowSelected: (NSNotification*) notification 
{
	//Get selected cell and filename
	UIImageAndTextTableCell* selectedCell = [_tableCells objectAtIndex: [_table selectedRow]];
	//NSLog(@"seletedCell title: %@",[selectedCell title]);
	
	[[_delegate getBdo] setState:[selectedCell title]];
	//Do stuff here...
	SearchZipResultView *_searchZipResultView = [SearchZipResultView alloc];
	
	//[_bdo setCountry:[selectedCell title]];
	
	[_searchZipResultView setZipcode:[selectedCell title]];
	[_searchZipResultView setCountry:[[_delegate getBdo] getCountry]];
	[_searchZipResultView setState:[[_delegate getBdo]  getState]];
	[_searchZipResultView fetchData];
	
	[_searchZipResultView setDelegate:self];
	[_searchZipResultView setPrevView:[self retain]];
	//[_searchNameResultView setCity:searchentry];
	[_searchZipResultView initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
													, _rect.size.width,
													_rect.size.height+49.0f) ];
	//_previousView = [_actualView retain];
	[_delegate setCurrentView: _searchZipResultView];
	
	//	NSLog(@"prevview: %@",[_delegate getPreviousView]);
	//	NSLog(@"actview: %@",[_delegate getCurrentView]);
	
	[[_delegate getTransitionView] transition:1 toView:_searchZipResultView];
	
	
}


@end





@implementation ZipOverView

-(void)fillTable:(id)p {
	
	[_tableCells release];
	_tableCells = [[NSMutableArray alloc] init];
	
	[_tableHeaders release];	
	_tableHeaders = [[NSMutableArray alloc] init];
	
	NSMutableDictionary* cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
	
	NSString *countstr = [[_delegate getBdo] getCountry];
	NSString *tableheaderstr = [countstr stringByAppendingString:@" - ZIP code overview"];
	
	[cellDict setObject:tableheaderstr forKey:@"title"];
	[cellDict setObject:[[NSNumber alloc] initWithInt:0] forKey:@"beginRow"];
	
	[_tableHeaders addObject: cellDict];
	[cellDict release];
	
	DataManager *dm = [DataManager alloc];
	
	SQLiteDB *sql = [[SQLiteDB alloc] initWithPath:[dm getDataBaseName:countstr]];
	
	[dm release];
	
	
	NSArray *nsa = [sql query:@"SELECT zipcode from zipcodes ORDER BY zipcode asc;"];
	//NSLog(@"array: %@",nsa);
	[sql release];
	NSString *previousnumber = @"";
	NSString *prevSection = @"";
	int i=0;
	int count = 0;
	for (i=0;i<[nsa count];i++) {
		
		NSString *twonumbers = [[[nsa objectAtIndex:i] objectForKey:@"zipcode"] substringWithRange:NSMakeRange(0,2)];
		
		if (![twonumbers isEqual:previousnumber]) {
			
			
			NSString *firstletter = [twonumbers substringWithRange:NSMakeRange(0,1)];
			
			
			
			if (![firstletter isEqual:prevSection]) {
				prevSection = [firstletter copy];
				NSLog(@"%@ - %d",firstletter,i);
				
				//	 NSString *tableheaderstr = [countstr stringByAppendingString:firstletter];
				NSMutableDictionary* cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
				
				[cellDict setObject:firstletter forKey:@"title"];
				[cellDict setObject:[[NSNumber alloc] initWithInt:count] forKey:@"beginRow"];
				
				[_tableHeaders addObject: cellDict];
				[cellDict release];
				
				
			}
			
			
			previousnumber = [twonumbers copy];
			//NSLog(@"%@ - %d",twonumbers,i);
			
			int strleng = [[[nsa objectAtIndex:i] objectForKey:@"zipcode"] length];
			
			int j=0;
			for (j=0;j<(strleng-2);j++) twonumbers = [twonumbers stringByAppendingString:@"."];
			
			//NSLog(@"object: %@",[[nsa objectAtIndex:i] objectForKey:@"state"]);
			UIImageAndTextTableCell* cell = [[UIImageAndTextTableCell alloc] init];
			[cell setTitle: twonumbers];
			[cell setDisclosureStyle:2];
			[cell setShowDisclosure:YES animated:NO];
			[_tableCells addObject: cell]; 
			[cell release];
			count++;
		}
		
	}
	
	
	
	_sectionList = [[UISectionList alloc] initWithFrame:CGRectMake(_rect.origin.x, _rect.origin.y, _rect.size.width, _rect.size.height) showSectionIndex:NO];
	[_sectionList setDataSource:self];
	
	
	_table = [_sectionList table];
	[_table setShouldHideHeaderInShortLists:NO];
	UITableColumn * packageColumn = [[UITableColumn alloc] initWithTitle:@"Title" identifier:@"title" width:320.0f];
	
	[_table addTableColumn:packageColumn];
	[_table setSeparatorStyle:1];
	[_table setRowHeight:48.0f];
	[_table setDelegate:self];
	[_table setAllowsReordering:NO];
	[_table reloadData];
	[_sectionList reloadData];
	
	[_sectionList setAlpha:0.0f];
	
	[packageColumn release];
	
	[self addSubview:_sectionList];
	
	
	UIAlphaAnimation* alpha = [[UIAlphaAnimation alloc] initWithTarget: _sectionList];
	[alpha setStartAlpha: 0.0f];
	[alpha setEndAlpha: 1.0f];
	[[[UIAnimator alloc] init] addAnimation:alpha withDuration:.8 start:YES];
	UIAlphaAnimation* alpha1 = [[UIAlphaAnimation alloc] initWithTarget: _progresstable];
	[alpha1 setStartAlpha: 1.0f];
	[alpha1 setEndAlpha: 0.0f];
	[[[UIAnimator alloc] init] addAnimation:alpha1 withDuration:.5 start:YES];
	[alpha release];
	[alpha1 release];
	[_uip release];
	[_progresstable release];
	
	
	
}

- (id)initWithFrame:(struct CGRect)rect {
	
	self = [super initWithFrame:rect];
	
	_rect = rect;
	
	_progresstable = [[UITable alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 415.0f)];//-45.0f)];
	
	_uip = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(150, 170, 20,20)];
	
	[_uip setStyle:1];
	[_progresstable addSubview:_uip];
	
	[self addSubview:_progresstable];
	[_uip startAnimation];
	
	[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(fillTable:) userInfo:nil repeats:NO];
	
	//	[[_delegate getNavBar] showLeftButton:@"Back" withStyle:2 rightButton:nil withStyle:0];	
	return self;
	
	
}

-(void)dealloc {
	[_tableHeaders release];
	[_tableCells release];
	//[_table release];
	[_sectionList release];	
	
	[super dealloc];
}

-(void)setDelegate:(id)del {
	//NSLog(@"delegate: %@",del);
	_delegate = del;
}

-(void)setPrevView:(UIView*)pv {
	_prevView = pv;
	//NSLog(@"NameStateView setPrevView: %@",_prevView);
	
}

-(UIView*)getPrevView {
	//NSLog(@"NameStateView getPrevView: %@",_prevView);
	return _prevView;
}

#pragma mark - delegated methods -

- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList {
	return [_tableHeaders count];
}

- (NSString *)sectionList:(UISectionList *)aSectionList titleForSection:(int)row {	
	return [[_tableHeaders objectAtIndex:row] objectForKey:@"title"];
}       

- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)row {
	// this is what determines the placement of the header
	return [[[_tableHeaders objectAtIndex:row] valueForKey:@"beginRow"] intValue];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
{
	return [_tableCells objectAtIndex: row];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
			   reusing: (BOOL) reusing
{
	//What does this message do?
    return nil;
}

- (int) numberOfRowsInTable: (UISectionTable*)table
{
	return [_tableCells count];
}

- (void) tableRowSelected: (NSNotification*) notification 
{
	//Get selected cell and filename
	UIImageAndTextTableCell* selectedCell = [_tableCells objectAtIndex: [_table selectedRow]];
	//NSLog(@"seletedCell title: %@",[selectedCell title]);
	
	
	//Do stuff here...
	ZipDetailView *zipdetailview = [ZipDetailView alloc];
	[zipdetailview setZipPrefix:[[selectedCell title]  substringWithRange:NSMakeRange(0,2)]];
	
	//[_bdo setCountry:[selectedCell title]];
	
	[zipdetailview setDelegate:_delegate];
	[zipdetailview setPrevView:[self retain]];
	[zipdetailview initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
											 , _rect.size.width,
											 _rect.size.height) ];
	//	[_delegate setPreviousView:[[_delegate getCurrentView] retain]];//_previousView = [_actualView retain];
	[_delegate setCurrentView: zipdetailview];
	
	//	NSLog(@"prevview: %@",[_delegate getPreviousView]);
	//	NSLog(@"actview: %@",[_delegate getCurrentView]);
	
	[[_delegate getTransitionView] transition:1 toView:zipdetailview];
	[zipdetailview release];
	
}


@end





@implementation NameCityView

- (void) fillTable:(id)p {
	_sectionList = [[UISectionList alloc] initWithFrame:CGRectMake(_rect.origin.x, _rect.origin.y, _rect.size.width, _rect.size.height) showSectionIndex:YES];
	[_sectionList setDataSource:self];
	
	
	_table = [_sectionList table];
	[_table setShouldHideHeaderInShortLists:NO];
	UITableColumn * packageColumn = [[UITableColumn alloc] initWithTitle:@"Title" identifier:@"title" width:320.0f];
	
	[_table addTableColumn:packageColumn];
	[_table setSeparatorStyle:1];
	[_table setRowHeight:48.0f];
	[_table setDelegate:self];
	[_table setAllowsReordering:NO];
	[_table reloadData];
	[_sectionList reloadData];
	
	[packageColumn release];
	
	
	[_tableCells release];
	_tableCells = [[NSMutableArray alloc] init];
	
	[_tableHeaders release];	
	_tableHeaders = [[NSMutableArray alloc] init];
	
	
	NSString *countstr = [[_delegate getBdo] getCountry];
	NSString *statestr = [[_delegate getBdo] getState];
	
	NSMutableDictionary* cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
	
	[cellDict setObject:[[countstr stringByAppendingString:@" - "] stringByAppendingString:statestr] forKey:@"title"];
	[cellDict setObject:[[NSNumber alloc] initWithInt:0] forKey:@"beginRow"];
	
	[_tableHeaders addObject: cellDict];
	[cellDict release];
	
	DataManager *dm = [DataManager alloc];
	
	SQLiteDB *sql = [[SQLiteDB alloc] initWithPath:[dm getDataBaseName:countstr]];
	
	[dm release];
	
	NSArray *nsa = [sql query:[NSString stringWithFormat:@"SELECT c.city from relations r, city c, states s where r.cityid = c.id and r.stateid = s.id and s.state = '%@' group by c.city ORDER BY c.city asc;",[[_delegate getBdo] getState]]];
	//NSLog(@"array: %@",nsa);
	[sql release];
	NSString *firstletter = @"";
	int i=0;
	int count=0;
	//NSLog(@"size: %d",[nsa count]);
	
	for (i=0;i<[nsa count];i++) {
		
		//if ([[[nsa objectAtIndex:i] objectForKey:@"city"] isEqual:@""]) continue;
		
		NSString *firtsl = [[[nsa objectAtIndex:i] objectForKey:@"city"] substringWithRange:NSMakeRange(0,1)];
		
		if (![firtsl isEqual:firstletter]) {
			firstletter = [firtsl copy];
			//NSLog(@"%@ - %d",firstletter,i);
			
			//	 NSString *tableheaderstr = [countstr stringByAppendingString:firstletter];
			NSMutableDictionary* cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
			
			[cellDict setObject:firstletter forKey:@"title"];
			[cellDict setObject:[[NSNumber alloc] initWithInt:count] forKey:@"beginRow"];
			
			[_tableHeaders addObject: cellDict];
			[cellDict release];
			
			
		}
		
		
		
		//NSLog(@"object: %@",[nsa objectAtIndex:i]);
		UIImageAndTextTableCell* cell = [[UIImageAndTextTableCell alloc] init];
		[cell setTitle: [[[nsa objectAtIndex:i] objectForKey:@"city"] capitalizedString]];
		// [cell setDisclosureStyle:2];
		[cell setShowDisclosure:NO animated:NO];
		[_tableCells addObject: cell]; 
		[cell release];
		
		count++;
		
		
	}
	
	[_sectionList reloadData];
	[_sectionList setAlpha:0.0f];
	[self addSubview:_sectionList];
	
	UIAlphaAnimation* alpha = [[UIAlphaAnimation alloc] initWithTarget: _sectionList];
	[alpha setStartAlpha: 0.0f];
	[alpha setEndAlpha: 1.0f];
	[[[UIAnimator alloc] init] addAnimation:alpha withDuration:.8 start:YES];
	UIAlphaAnimation* alpha1 = [[UIAlphaAnimation alloc] initWithTarget: _progresstable];
	[alpha1 setStartAlpha: 1.0f];
	[alpha1 setEndAlpha: 0.0f];
	[[[UIAnimator alloc] init] addAnimation:alpha1 withDuration:.5 start:YES];
	[alpha release];
	[alpha1 release];
	[_uip release];
	[_progresstable release];
	
	
}

- (id)initWithFrame:(struct CGRect)rect {
	
	self = [super initWithFrame:rect];
	
	_rect = rect;
	
	_progresstable = [[UITable alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 415.0f)];//-45.0f)];
	
	_uip = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(150, 170, 20,20)];
	
	[_uip setStyle:1];
	[_progresstable addSubview:_uip];
	
	[self addSubview:_progresstable];
	[_uip startAnimation];
	
	[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(fillTable:) userInfo:nil repeats:NO];
	//	[[_delegate getNavBar] showLeftButton:@"Back" withStyle:2 rightButton:nil withStyle:0];	
	return self;
	
	
}

-(void)dealloc {
	[_tableHeaders release];
	[_tableCells release];
	//[_table release];
	[_sectionList release];	
	
	[super dealloc];
}

-(void)setDelegate:(id)del {
	//NSLog(@"delegate: %@",del);
	_delegate = del;
}

-(void)setPrevView:(UIView*)pv {
	_prevView = pv;
	//NSLog(@"NameStateView setPrevView: %@",_prevView);
	
}

-(UIView*)getPrevView {
	//NSLog(@"NameStateView getPrevView: %@",_prevView);
	return _prevView;
}

#pragma mark - delegated methods -

- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList {
	return [_tableHeaders count];
}

- (NSString *)sectionList:(UISectionList *)aSectionList titleForSection:(int)row {	
	return [[_tableHeaders objectAtIndex:row] objectForKey:@"title"];
}       

- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)row {
	// this is what determines the placement of the header
	return [[[_tableHeaders objectAtIndex:row] valueForKey:@"beginRow"] intValue];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
{
	return [_tableCells objectAtIndex: row];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
			   reusing: (BOOL) reusing
{
	//What does this message do?
    return nil;
}

- (int) numberOfRowsInTable: (UISectionTable*)table
{
	return [_tableCells count];
}

- (void) tableRowSelected: (NSNotification*) notification 
{
	//Get selected cell and filename
	UIImageAndTextTableCell* selectedCell = [_tableCells objectAtIndex: [_table selectedRow]];
	//NSLog(@"seletedCell title: %@",[selectedCell title]);
	
	//[_searchNameResultView release];
	SearchNameResultView *_searchNameResultView = [SearchNameResultView alloc];
	
	//[_bdo setCountry:[selectedCell title]];
	
	[_searchNameResultView setCity:[selectedCell title]];
	[_searchNameResultView setCountry:[[_delegate getBdo] getCountry]];
	[_searchNameResultView setState:[[_delegate getBdo]  getState]];
	[_searchNameResultView fetchData];
	
	[_searchNameResultView setDelegate:self];
	[_searchNameResultView setPrevView:[self retain]];
	//[_searchNameResultView setCity:searchentry];
	[_searchNameResultView initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
													 , _rect.size.width,
													 _rect.size.height+49.0f) ];
	//_previousView = [_actualView retain];
	[_delegate setCurrentView: _searchNameResultView];
	
	//	NSLog(@"prevview: %@",[_delegate getPreviousView]);
	//	NSLog(@"actview: %@",[_delegate getCurrentView]);
	
	[[_delegate getTransitionView] transition:1 toView:_searchNameResultView];
	
	
	//Do stuff here...
	
}


@end






@implementation NameStateView

- (id)initWithFrame:(struct CGRect)rect {
	
	self = [super initWithFrame:rect];
	
	_rect = rect;
	
	[_tableCells release];
	_tableCells = [[NSMutableArray alloc] init];
	
	[_tableHeaders release];	
	_tableHeaders = [[NSMutableArray alloc] init];
	
	NSMutableDictionary* cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
	
	NSString *countstr = [[_delegate getBdo] getCountry];
	NSString *tableheaderstr = [countstr stringByAppendingString:@" - States"];
	
	[cellDict setObject:tableheaderstr forKey:@"title"];
	[cellDict setObject:[[NSNumber alloc] initWithInt:0] forKey:@"beginRow"];
	
	[_tableHeaders addObject: cellDict];
	
	
	DataManager *dm = [DataManager alloc];
	
	SQLiteDB *sql = [[SQLiteDB alloc] initWithPath:[dm getDataBaseName:countstr]];
	
	[dm release];
	
	NSArray *nsa = [sql query:@"SELECT state from states ORDER BY state asc;"];
	//NSLog(@"array: %@",nsa);
	[sql release];
	
	int i=0;
	for (i=0;i<[nsa count];i++) {
		
		//NSLog(@"object: %@",[[nsa objectAtIndex:i] objectForKey:@"state"]);
		UIImageAndTextTableCell* cell = [[UIImageAndTextTableCell alloc] init];
		[cell setTitle: [[nsa objectAtIndex:i] objectForKey:@"state"]];
		[cell setDisclosureStyle:2];
		[cell setShowDisclosure:YES animated:NO];
		[_tableCells addObject: cell]; 
		[cell release];
		
		
		
	}
	
	
	
	_sectionList = [[UISectionList alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height) showSectionIndex:NO];
	[_sectionList setDataSource:self];
	
	[self addSubview:_sectionList];
	
	_table = [_sectionList table];
	[_table setShouldHideHeaderInShortLists:NO];
	UITableColumn * packageColumn = [[UITableColumn alloc] initWithTitle:@"Title" identifier:@"title" width:320.0f];
	
	[_table addTableColumn:packageColumn];
	[_table setSeparatorStyle:1];
	[_table setRowHeight:48.0f];
	[_table setDelegate:self];
	[_table setAllowsReordering:NO];
	[_table reloadData];
	[_sectionList reloadData];
	
	[packageColumn release];
	
	//	[[_delegate getNavBar] showLeftButton:@"Back" withStyle:2 rightButton:nil withStyle:0];	
	return self;
	
	
}

-(void)dealloc {
	[_tableHeaders release];
	[_tableCells release];
	//[_table release];
	[_sectionList release];	
	
	[super dealloc];
}

-(void)setDelegate:(id)del {
	//NSLog(@"delegate: %@",del);
	_delegate = del;
}

-(void)setPrevView:(UIView*)pv {
	_prevView = pv;
	//NSLog(@"NameStateView setPrevView: %@",_prevView);
	
}

-(UIView*)getPrevView {
	//NSLog(@"NameStateView getPrevView: %@",_prevView);
	return _prevView;
}

#pragma mark - delegated methods -

- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList {
	return [_tableHeaders count];
}

- (NSString *)sectionList:(UISectionList *)aSectionList titleForSection:(int)row {	
	return [[_tableHeaders objectAtIndex:row] objectForKey:@"title"];
}       

- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)row {
	// this is what determines the placement of the header
	return [[[_tableHeaders objectAtIndex:row] valueForKey:@"beginRow"] intValue];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
{
	return [_tableCells objectAtIndex: row];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
			   reusing: (BOOL) reusing
{
	//What does this message do?
    return nil;
}

- (int) numberOfRowsInTable: (UISectionTable*)table
{
	return [_tableCells count];
}

- (void) tableRowSelected: (NSNotification*) notification 
{
	//Get selected cell and filename
	UIImageAndTextTableCell* selectedCell = [_tableCells objectAtIndex: [_table selectedRow]];
	//NSLog(@"seletedCell title: %@",[selectedCell title]);
	
	[[_delegate getBdo] setState:[selectedCell title]];
	//Do stuff here...
	NameCityView *namecityview = [NameCityView alloc];
	
	//[_bdo setCountry:[selectedCell title]];
	
	[namecityview setDelegate:_delegate];
	[namecityview setPrevView:[self retain]];
	[namecityview initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
											, _rect.size.width,
											_rect.size.height) ];
	//	[_delegate setPreviousView:[[_delegate getCurrentView] retain]];//_previousView = [_actualView retain];
	[_delegate setCurrentView: namecityview];
	
	//	NSLog(@"prevview: %@",[_delegate getPreviousView]);
	//	NSLog(@"actview: %@",[_delegate getCurrentView]);
	
	[[_delegate getTransitionView] transition:1 toView:namecityview];
	[namecityview release];
	
}


@end






@implementation NameZipView

- (id)initWithFrame:(struct CGRect)rect {
	
	self = [super initWithFrame:rect];
	
	_rect = rect;
	
	[_tableCells release];
	_tableCells = [[NSMutableArray alloc] init];
	
	[_tableHeaders release];	
	_tableHeaders = [[NSMutableArray alloc] init];
	
	NSMutableDictionary* cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
	[cellDict setObject:[[_delegate getBdo] getCountry] forKey:@"title"];
	[cellDict setObject:[[NSNumber alloc] initWithInt:0] forKey:@"beginRow"];
	
	[_tableHeaders addObject: cellDict];
	
	
	UIImageAndTextTableCell* cell = [[UIImageAndTextTableCell alloc] init];
	[cell setTitle: @"By names"];
	[cell setDisclosureStyle:2];
	[cell setShowDisclosure:YES animated:NO];
	[_tableCells addObject: cell]; 
	[cell release];
	
	cell = [[UIImageAndTextTableCell alloc] init];
	[cell setTitle: @"By ZIP codes"];
	[cell setDisclosureStyle:2];
	[cell setShowDisclosure:YES animated:NO];
	[_tableCells addObject: cell]; 
	[cell release];
	
	_sectionList = [[UISectionList alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height) showSectionIndex:NO];
	[_sectionList setDataSource:self];
	[_sectionList reloadData];
	[self addSubview:_sectionList];
	
	_table = [_sectionList table];
	[_table setShouldHideHeaderInShortLists:NO];
	UITableColumn * packageColumn = [[UITableColumn alloc] initWithTitle:@"Title" identifier:@"title" width:320.0f];
	
	[_table addTableColumn:packageColumn];
	[_table setSeparatorStyle:1];
	[_table setRowHeight:48.0f];
	[_table setDelegate:self];
	[_table setAllowsReordering:NO];
	
	[packageColumn release];
	
	[[_delegate getNavBar] showLeftButton:@"Back" withStyle:2 rightButton:nil withStyle:0];	
	return self;
	
	
}

-(void)dealloc {
	[_tableHeaders release];
	[_tableCells release];
	//[_table release];
	[_sectionList release];	
	
	[super dealloc];
}

-(void)setDelegate:(id)del {
	//NSLog(@"delegate: %@",del);
	_delegate = del;
}

-(void)setPrevView:(UIView*)pv {
	_prevView = pv;
}

-(UIView*)getPrevView {
	return _prevView;
}

#pragma mark - delegated methods -

- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList {
	return [_tableHeaders count];
}

- (NSString *)sectionList:(UISectionList *)aSectionList titleForSection:(int)row {	
	return [[_tableHeaders objectAtIndex:row] objectForKey:@"title"];
}       

- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)row {
	// this is what determines the placement of the header
	return [[[_tableHeaders objectAtIndex:row] valueForKey:@"beginRow"] intValue];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
{
	return [_tableCells objectAtIndex: row];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
			   reusing: (BOOL) reusing
{
	//What does this message do?
    return nil;
}

- (int) numberOfRowsInTable: (UISectionTable*)table
{
	return [_tableCells count];
}

- (void) tableRowSelected: (NSNotification*) notification 
{
	//Get selected cell and filename
	UIImageAndTextTableCell* selectedCell = [_tableCells objectAtIndex: [_table selectedRow]];
	//NSLog(@"seletedCell title: %@",[selectedCell title]);
	
	if ([_table selectedRow] == 0) {
		
		
		NameStateView *namestateview = [NameStateView alloc];
		
		//[_bdo setCountry:[selectedCell title]];
		
		[namestateview setDelegate:_delegate];
		[namestateview setPrevView:[self retain]];
		[namestateview initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
												 , _rect.size.width,
												 _rect.size.height) ];
		//	[_delegate setPreviousView:[[_delegate getCurrentView] retain]];//_previousView = [_actualView retain];
		[_delegate setCurrentView: namestateview];
		
		//	NSLog(@"prevview: %@",[_delegate getPreviousView]);
		//	NSLog(@"actview: %@",[_delegate getCurrentView]);
		
		[[_delegate getTransitionView] transition:1 toView:namestateview];
		[namestateview release];
	} else if ([_table selectedRow] == 1) {
		
		
		ZipOverView *zipoverview = [ZipOverView alloc];
		
		//[_bdo setCountry:[selectedCell title]];
		
		[zipoverview setDelegate:_delegate];
		[zipoverview setPrevView:[self retain]];
		[zipoverview initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
											   , _rect.size.width,
											   _rect.size.height) ];
		//	[_delegate setPreviousView:[[_delegate getCurrentView] retain]];//_previousView = [_actualView retain];
		[_delegate setCurrentView: zipoverview];
		
		//	NSLog(@"prevview: %@",[_delegate getPreviousView]);
		//	NSLog(@"actview: %@",[_delegate getCurrentView]);
		
		[[_delegate getTransitionView] transition:1 toView:zipoverview];
		[zipoverview release];
	}
	//Do stuff here...
	
}


@end







@implementation BrowseView

- (id)initWithFrame:(struct CGRect)rect {
	
	DataManager *dm = [DataManager alloc];
	
	NSArray *installedCountries = [dm getInstalledCountries];
	
	[dm release];
	
	self = [super initWithFrame:rect];
	_rect = rect;
	_navBar  = [[UINavigationBar alloc] init];
	[_navBar setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 48.0f)];
	[_navBar setDelegate:self];
	[_navBar enableAnimation];
	
	[_navBar pushNavigationItem: [[UINavigationItem alloc] initWithTitle: @"Browse"]];	
	
	[self addSubview:_navBar];
	
	_transitionView =  [ [ UITransitionView alloc ] 
						initWithFrame:
						CGRectMake(rect.origin.x, rect.origin.y+48.0f, rect.size.width,
								   rect.size.height - 49.0f)
						];
	[self addSubview:_transitionView];
	[_tableCells release];
	_tableCells = [[NSMutableArray alloc] init];
	
	[_tableHeaders release];	
	_tableHeaders = [[NSMutableArray alloc] init];
	
	
	NSMutableDictionary* cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
	[cellDict setObject:@"Countries" forKey:@"title"];
	[cellDict setObject:[[NSNumber alloc] initWithInt:0] forKey:@"beginRow"];
	
	[_tableHeaders addObject: cellDict];
	
	int i=0;
	for (i=0;i<[installedCountries count];i++) {	
		UIImageAndTextTableCell* cell = [[UIImageAndTextTableCell alloc] init];
		[cell setTitle: [installedCountries objectAtIndex:i]];
		[cell setDisclosureStyle:2];
		[cell setShowDisclosure:YES animated:NO];
		[_tableCells addObject: cell]; 
		[cell release];
	}	
	
	_sectionList = [[UISectionList alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height) showSectionIndex:NO];
	[_sectionList setDataSource:self];
	[_sectionList reloadData];
	[_transitionView transition:0  toView:_sectionList];
	
	
	_table = [_sectionList table];
	[_table setShouldHideHeaderInShortLists:NO];
	UITableColumn * packageColumn = [[UITableColumn alloc] initWithTitle:@"Title" identifier:@"title" width:320.0f];
	
	[_table addTableColumn:packageColumn];
	[_table setSeparatorStyle:1];
	[_table setRowHeight:48.0f];
	[_table setDelegate:self];
	[_table setAllowsReordering:NO];
	
	
	_actualView = _sectionList;
	
	[_bdo release];
	_bdo = [[BrowseDataObject alloc] init];
	
	return self;
	
	
}

-(void)dealloc {
	[_tableHeaders release];
	[_tableCells release];
	[_sectionList release];	
	[_navBar release];
	[_transitionView release];
	
	[super dealloc];
}

-(BrowseDataObject*)getBdo {
	return _bdo;
}

-(UINavigationBar*)getNavBar {
	return _navBar;
}

-(void)setDelegate:(id)del {
	//NSLog(@"delegate: %@",del);
	_delegate = del;
}

-(UITransitionView*)getTransitionView {
	return _transitionView;
}

-(UIView*)getCurrentView {
	return _actualView;
}

-(void)setCurrentView:(UIView*)view {
	_actualView = view;
	//NSLog(@"setCurrentView: %@",view);
}

#pragma mark - delegated methods -


- (void)navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
	switch (button) {
		case 0:
			//NSLog(@"navbar button 0");
			break;
		case 1:
			//NSLog(@"navar button 1");
			//NSLog(@"actualView: %@",_actualView);
			//NSLog(@"prevView: %@",[_actualView getPrevView]);
			if ([_actualView getPrevView] == _sectionList) {
				[_navBar showLeftButton:nil withStyle:2 rightButton:nil withStyle:0];
			}
			[_transitionView transition:2 fromView:_actualView toView:[_actualView getPrevView]];
			
			_actualView = [_actualView getPrevView];
			//	if (_actualView == namezipview) _previousView = _sectionList;
			
			break;
	}
}

- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList {
	return [_tableHeaders count];
}

- (NSString *)sectionList:(UISectionList *)aSectionList titleForSection:(int)row {	
	return [[_tableHeaders objectAtIndex:row] objectForKey:@"title"];
}       

- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)row {
	// this is what determines the placement of the header
	return [[[_tableHeaders objectAtIndex:row] valueForKey:@"beginRow"] intValue];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
{
	return [_tableCells objectAtIndex: row];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
			   reusing: (BOOL) reusing
{
	//What does this message do?
    return nil;
}

- (int) numberOfRowsInTable: (UISectionTable*)table
{
	return [_tableCells count];
}

- (void) tableRowSelected: (NSNotification*) notification 
{
	//Get selected cell and filename
	UIImageAndTextTableCell* selectedCell = [_tableCells objectAtIndex: [_table selectedRow]];
	//NSLog(@"seletedCell title: %@",[selectedCell title]);
	
	namezipview = [NameZipView alloc];
	
	[_bdo setCountry:[selectedCell title]];
	
	[namezipview setDelegate:self];
	[namezipview setPrevView:_sectionList];
	[namezipview initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
										   , _rect.size.width,
										   _rect.size.height - 49.0f) ];
	//_previousView = [_actualView retain];
	_actualView = namezipview;
	
	[_transitionView transition:1 toView:namezipview];
	[namezipview release];
	/*
	 SQLiteDB *sql = [[SQLiteDB alloc] initWithPath:@"/Applications/ZIPcodes.app/germany.dat"];
	 
	 NSArray *nsa = [sql query:@"SELECT * from states ORDER BY state asc;"];
	 NSLog(@"array: %@",nsa);
	 [sql release];
	 */
	//Do stuff here...
	
}


@end
